﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SilverlightAndAspdotNetMVC.Web.Models;

namespace SilverlightAndAspdotNetMVC.Web.Daos
{
    public class MonitorDataDao
    {
        DataClassesTable2DataContext dc = new DataClassesTable2DataContext();

        public MonitorDataEntity SelectMonitorData2(string zoneId)
        {
            MonitorTable2 selectTable = dc.MonitorTable2.Where(entity => entity.zone_id == zoneId).FirstOrDefault();
            MonitorDataEntity mde = null;
            if (selectTable == null)
            {
                return mde;
            }
            mde = new MonitorDataEntity();
            mde.ZoneId = selectTable.zone_id;
            mde.ExportTime = selectTable.export_time;
            mde.Humidity = ParseDecimalToDouble(selectTable.humidity);
            mde.Illumination = ParseDecimalToDouble(selectTable.illumination);
            mde.Temperature = ParseDecimalToDouble(selectTable.temperature);
            return mde;
        }

        public MonitorDataEntity SelectMonitorData(string zoneId)
        {
            FileWriter fw = new FileWriter();
            try
            {
                MonitorDataEntity mde = fw.Read("c:\\file2.txt");
                mde.Success = true;
                return mde;
            }
            catch
            {
                MonitorDataEntity mde = new MonitorDataEntity()
                {
                    Success = false,
                    ErrorMessage = "文件读取失败！"
                };
                return mde;
            }
        }

        private double ParseDecimalToDouble(decimal? dec)
        {
            if (!dec.HasValue)
            {
                return 0d;
            }
            return double.Parse(dec.Value.ToString());
        }
    }
}